import sys
import os

file_path = os.path.abspath(__file__)
end = file_path.index('qmt_auto_trade') + 17
project_path = file_path[0:end]
sys.path.append(project_path)

from loguru import logger
from apscheduler.schedulers.blocking import BlockingScheduler
import login.qmt_auto_login_service as qmt_auto_login_service
import strategy.auto_buy_servie as auto_buy_servie
import trade.trade_service as trade_service

# # 定义BlockingScheduler
blockingScheduler = BlockingScheduler()


# qmt自动登录
def qmt_auto_login():
    qmt_auto_login_service.qmt_auto_login()


# 策略运行
def strategy_auto_buy():
    logger.info('策略开始运行')
    auto_buy_servie.strategy_run(False)


# 同步持仓
def sync_self_position():
    trade_service.sync_self_position()


# qmt自动登录
# blockingScheduler.add_job(qmt_auto_login, 'cron', hour='08', minute='30')

# 同步持仓
blockingScheduler.add_job(qmt_auto_login, 'cron', hour='09', minute='01')

# 策略运行时间上午
blockingScheduler.add_job(strategy_auto_buy, 'cron', hour='09', minute='30')

# 策略运行时间下午
blockingScheduler.add_job(strategy_auto_buy, 'cron', hour='13', minute='00')
logger.info('定时任务启动成功')
blockingScheduler.start()
